/*
 *
 *  Copyright (C) 2009 GSyC/LibreSoft
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@gsyc.es>
 *
 */

package com.LibreGeoSocial.App;

import java.util.ArrayList;
import java.util.Collections;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.LibreGeoSocial.Api.GeoNodePositionComparator;
import com.LibreGeoSocial.Api.Group;
import com.LibreGeoSocial.Api.LibreGeoSocial;
import com.LibreGeoSocial.App.Resources.ResourceLGS;
import com.LibreGeoSocial.App.Routes.LocationService;
import com.LibreGeoSocial.App.Utils.LocationUtils;

public class GroupList extends ListActivity {
	
	
	// Context menu
	private static final int MENU_VIEW_MEMBERS = Menu.FIRST + 1;
	private static final int MENU_VIEW_GROUP = Menu.FIRST + 2;
	
	
	static private ArrayList<Group> mList;
	
	GroupAdapter mAdapter; 

	public static Group group;
	
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);		  		
		
		mList = LibreGeoSocial.getInstance().getMyGroups();
		
		GeoNodePositionComparator comparator = new GeoNodePositionComparator();
		Collections.sort(mList, comparator);
		
  		// Adapter for ListView
        mAdapter = new GroupAdapter(this);
        setListAdapter(mAdapter);
        
        if ( mList.size()==0 )
        {
        	
    		Toast.makeText(getBaseContext(), 
    					   "In this moment, you aren't in a dynamic groups!", 
    					   Toast.LENGTH_LONG).show();        	
        }
        
        registerForContextMenu(this.getListView());
		
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View view,  ContextMenuInfo menuInfo)
	{ 

		menu.add (0, MENU_VIEW_MEMBERS, 0, "View Members Group");
		menu.add (0, MENU_VIEW_GROUP, 0, "View Position Group");         
		
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) 
	{
		
		
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		group = (Group) mList.get(info.position);
		
		switch (item.getItemId()) {
		
			case MENU_VIEW_MEMBERS:
						
				Intent i = new Intent(this, ResourceLGS.class);
				
				i.putExtra("group", group.getId() );
				
				startActivity(i);
				
				break;
				
			case MENU_VIEW_GROUP:
				
		    	Intent i2 = new Intent(this, ViewMap.class);
		    	
		    	i2.putExtra("latitude",group.getLatitude());
		    	i2.putExtra("longitude",group.getLongitude());
		    	i2.putExtra("url","");
		    	
		    	startActivity(i2);
					
				break;
		
		}
		
		return true;
	}
	
	
	public static class GroupAdapter extends BaseAdapter {

		private Context mContext;
		
		public GroupAdapter (Context c) {
            mContext = c;
        }

		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view;
			
	        if (convertView == null) {
	            // Make up a new view
	            LayoutInflater inflater = (LayoutInflater) mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            view = inflater.inflate(R.layout.generic_list, null);
	        } else {
	            // Use convertView if it is available
	            view = convertView;
	        }
			
	        
	        Group group = (Group) mList.get(position);	       	       

	        ImageView img = (ImageView) view.findViewById(R.id.image);
	        img.setImageResource(R.drawable.paper_pencil_48);
			
			TextView t = (TextView) view.findViewById(R.id.title);
			t.setText(group.getGroupName());
			
			String distString = "";
			
			// Calculate distance
			if ( group.getLatitude()==-1.0 && 
					group.getLongitude()==-1.0)
			{
				distString = "Unknow Location";
			}
			else
			{
				float dist = LocationUtils.calculateDistance (LocationService.getCurrentLocation(), 
															  group.getLocation());
				
				distString = LocationUtils.displayDistance(dist, LocationUtils.AUTO);
			}
			
			t = (TextView) view.findViewById(R.id.owner);
			t.setText("Dist: " + distString);
	        
			return view;
	        
		}

	}
	
}
